/**
 * Created by Administrator on 15-11-4.
 */
window.onload=function(){
    var supplierId = $.getUrlParam("supplierId");
    var url = getUrl();
    var addrId = $.getUrlParam("addrId");
    var userId = getUserId();
    var storage=window.localStorage;
    var num=$(".Amount").val();
    var len = 0;
    storage.setItem("num",num);
    $.jsonp({
        url: 'http://'+url+'/api/supplier/supplier_detail-'+supplierId,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            var complied_tpl = juicer($("#menu").text());
            var html = complied_tpl.render(data);
            $(".top").append(html);
            global_price=data.supplier.price;
            $(".ui-txt-highlight").text(global_price+"元");
        },
        error: function (xOptions, textStatus) {
        }
    });
    $.jsonp({
        url: 'http://'+url+'/api/user/user_address_list',
        data: {userId:userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $("#all_pj").attr("href","dizhi.html?supplierId="+supplierId);
            for(;len<data.arrays.length;len++){
                var addrId = data.arrays[data.arrays.length-1];
                if($.getUrlParam("addrId")){
                    addrId = $.getUrlParam("addrId");
                    if(data.arrays[len].id==addrId){
                        var ele = data.arrays[len];
                        var html = '<div class="addrId" id='+ele.id+'>'+ele.realName+'&nbsp&nbsp'+ele.telphone+'&nbsp&nbsp'+ele.areaCode+'<br/>'+ele.address+'</div>';
                    }
                }else{
                    var ele = data.arrays[data.arrays.length-1];
                    var html = '<div class="addrId" id='+ele.id+'>'+ele.realName+'&nbsp&nbsp'+ele.telphone+'&nbsp&nbsp'+ele.areaCode+'<br/>'+ele.address+'</div>';
                }

            }

            $("#addrmore").append(html);
        },
        error: function (xOptions, textStatus) {
        }
    });

    $(document).on("click",".ui-btn-lg",function(){
        var addressId = $(".addrId").attr("id");
        var number = $(".Amount").val();
        var telephone = $(".tele").val();
        var retype = $.trim($("input:checked").parent().text());
        if(telephone=="")
        {
            showToast("手机号不能为空~");
            return;
        }
        if(telephone.length!=11)
        {
            showToast("手机号格式不对~");
            return;
        }
        if(addressId == "" || addressId == undefined){
            showToast("收货地址不能为空~");
            return;
        }
        if(retype == ""){
            showToast("请选择收货方式~");
            return;
        }
        $.jsonp({
            url: 'http://'+url+'/api/order/submit_order',
            data: {supplierId:supplierId,number:number,buyTelphone:telephone,addressId:addressId,userId:userId},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                window.location = "yiren_zhifudingdan.html?orderId="+data.orderId+"&retype="+retype;
               // $("#commit").attr("href","yiren_zhifudingdan.html?orderId="+data.orderId+"&retype="+retype);
            },
            error: function (xOptions, textStatus) {
            }
        });
    });

    (function(){
        if(storage.getItem("num")){
            $(".Amount").val(storage.getItem("num"));

        }
    })();
    $(document).on("click",".Increase",function(){
        calc();
        $("#shuliang").text($(".Amount").val());
    })
    $(document).on("click",".Decrease",function(){
        calc();
        $("#shuliang").text($(".Amount").val());
    })
    $(document).on("click",".DisDe",function(){
        calc();
        $("#shuliang").text($(".Amount").val());
    })

    function calc(){
        var amount=parseInt($(".Amount").val())*global_price;
        $(".ui-txt-highlight").text(amount+"元");

    }


}